//
//  ForGoPasswordViewController.m
//  Logistics
//
//  Created by 华谕 on 2017/11/1.
//  Copyright © 2017年 HUYU. All rights reserved.
//

#import "ForGoPasswordViewController.h"

static int seconds = 60;//计时60s
@interface ForGoPasswordViewController ()<UITextFieldDelegate>
{
    UITextField *phoneField;
    UITextField *passwordField;
    UITextField *sourepassField;
    UITextField *codeField;
    UIButton *codeBtn;
    NSTimer *timer;
}

@end

@implementation ForGoPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
}

- (void)creatUI
{
    self.view.backgroundColor = RGB(245, 245, 245);
    self.navigationItem.title = _titleString;
    self.navigationController.navigationBar.translucent = NO;
    NSArray *imgAry = @[@"my_icon04",@"my_icon05",@"my_icon05",@"my_icon06"];
    NSArray *pladerAry = @[@"手机号",@"新密码",@"确认密码",@"验证码"];
    for (int i = 0; i < pladerAry.count; i ++)
    {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth, 20*displayheight+i*55*displayheight, SCREEN_WIDTH-30*displaywidth, 45*displayheight)];
        backView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:backView];
        UIImageView *imageview = [ZCControl createImageViewWithFrame:CGRectMake(5*displaywidth, 12.5*displayheight, 20*displaywidth, 20*displayheight) ImageName:imgAry[i]];
        imageview.contentMode = UIViewContentModeScaleAspectFit;
        [backView addSubview:imageview];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(35*displaywidth, 12.5*displayheight, 1, 20*displayheight)];
        line.backgroundColor = RGB(204, 204, 204);
        [backView addSubview:line];
        UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(40*displaywidth, 2.5*displayheight, SCREEN_WIDTH-70*displaywidth, 40*displayheight) placeholder:pladerAry[i] passWord:NO leftImageView:nil rightImageView:nil Font:15.0];
        [textfield setValue:RGB(135, 135, 135) forKeyPath:@"_placeholderLabel.textColor"];
        textfield.delegate = self;
        textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
        textfield.textColor = RGB(48, 48, 48);
        textfield.textAlignment = NSTextAlignmentLeft;
        [backView addSubview:textfield];
        if (i == 0)
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            phoneField = textfield;
        }
        else if (i == 1)
        {
            textfield.secureTextEntry = YES;
            passwordField = textfield;
        }
        else if (i == 2)
        {
            textfield.secureTextEntry = YES;
            sourepassField = textfield;
        }
        else
        {
            textfield.keyboardType = UIKeyboardTypeNumberPad;
            textfield.frame = CGRectMake(40*displaywidth, 2.5*displayheight, SCREEN_WIDTH-170*displaywidth, 40*displayheight);
            codeField = textfield;
            codeBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH-130*displayheight, 10*displayheight, 90*displayheight, 25*displayheight) ImageName:nil Target:self Action:@selector(verifiBtnAction:) Title:@"获取验证码" Font:12.0];
            [codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            codeBtn.backgroundColor = RGB(255, 153, 20);
            [codeBtn.layer setCornerRadius:4];
            [codeBtn.layer setMasksToBounds:YES];
            [backView addSubview:codeBtn];
        }
    }
    
    UIButton *submitBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 250*displayheight, SCREEN_WIDTH-30*displaywidth, 45*displayheight) ImageName:nil Target:self Action:@selector(submitAction) Title:@"提交" Font:18.0];
    submitBtn.backgroundColor = RGB(255, 153, 20);
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitBtn.layer.cornerRadius = 5;
    submitBtn.layer.masksToBounds = YES;
    [self.view addSubview:submitBtn];
    
}

- (void)submitAction
{
    [self.view endEditing:YES];
    if (phoneField.text.length != 11)
    {
        [LTools showMBProgressWithText:@"请输入手机号码" addToView:self.view];
        return;
    }
    if ([passwordField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请设置密码" addToView:self.view];
        return;
    }
    if ([sourepassField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请确认密码" addToView:self.view];
        return;
    }
    if (![sourepassField.text isEqualToString:passwordField.text])
    {
        [LTools showMBProgressWithText:@"密码不一致" addToView:self.view];
        return;
    }
    if ([codeField.text isEqualToString:@""])
    {
        [LTools showMBProgressWithText:@"请输入验证码" addToView:self.view];
        return;
    }
    WEAKSELF;
    NSDictionary *dic = @{@"phone":phoneField.text,
                          @"password":passwordField.text,
                          @"code":codeField.text
                          };
    [HYBaseHandler requestChangePassParmers:dic Success:^(id obj)
    {
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"修改成功" addToView:sharedDelegate.window];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:sharedDelegate.window];
        }
        
    } failed:^(NSError *error) {
        
    }];
}

- (void)verifiBtnAction:(UIButton *)btn
{
    if (phoneField.text.length < 1)
    {
        [LTools showMBProgressWithText:@"请输入手机号" addToView:self.view];
        return;
    }
    if (phoneField.text.length != 11)
    {
        [LTools showMBProgressWithText:@"请输入正确的手机号" addToView:self.view];
        return;
    }
    [self requestRegisCode4register];
    
}

//修改密码验证码
- (void)requestRegisCode4register
{
    [self.view endEditing:YES];
    WEAKSELF;
    NSDictionary *dic = @{@"phone":phoneField.text};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestForgoPassParmers:dic Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             [LTools showMBProgressWithText:@"验证码已发送" addToView:weakSelf.view];
             [weakSelf startTimer];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}
- (void)startTimer
{
    seconds = 60;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(calculateTime) userInfo:Nil repeats:YES];
    codeBtn.userInteractionEnabled = NO;
}
//计算时间
- (void)calculateTime
{
    NSString *title = [NSString stringWithFormat:@"(%dS)重新获取",seconds];
    codeBtn.titleLabel.text = title;
    [codeBtn setTitle:title forState:UIControlStateNormal];
    
    if (seconds != 0) {
        seconds --;
    }else
    {
        [self renewTimer];
    }
    
}
//计时器归零
- (void)renewTimer
{
    [timer invalidate];//计时器停止
    codeBtn.userInteractionEnabled = YES;
    [codeBtn setTitle:@"重新获取验证码" forState:UIControlStateNormal];
    seconds = 60;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
